package com.jlkf.common.redis.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

import com.jlkf.pojo.CarCaculationPointCharges;
import com.jlkf.service.util.Distance;
import com.jlkf.service.util.StringTools;

import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


public class JedisTest {

	@Test
	public void testSpringJedisCluster() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/spring-*.xml");
		JedisPool jedisCluster =  (JedisPool) applicationContext.getBean("redisClient");
		Jedis jedis=null;
		if(jedis==null){
			jedis=jedisCluster.getResource();
		}
		Long orderId=0l;
		
		System.out.println(jedis.ping());
		jedis.del("configManager");
//		jedis.del("configManager");
//		System.out.println(jedis.get("configManager"));
//		 JSONArray ja1 = JSONArray.fromObject("[\"113.944931,22.558051,303.640533\",\"113.944931,22.558051,303.640533\",\"113.944931,22.558051,303.640533\",\"113.944938,22.558074,303.640533\",\"113.944938,22.558074,303.640533\"]");
//		 List<String> StringList = JSONArray.toList(ja1, String.class);
		 
		/*if(!StringUtils.isEmpty(line)){//如果有线的数据
			JSONArray ja_line_list = JSONArray.fromObject(line);
			List<CarCaculationPointCharges> ccpclist=JSONArray.toList(ja_line_list, CarCaculationPointCharges.class);//获取走过收费点路段的记录
			  for(String str:StringList){//查询当前的我走过的经纬度 是否在我的收费段
				   String[] driverpos = str.split(",");
					double driverpos1=Double.parseDouble(driverpos[0]);double driverpos2=Double.parseDouble(driverpos[1]);//目前司机上传的多个经纬度
				   for(CarCaculationPointCharges cc:ccpclist){//遍历我的收费表中的数据
						String position =cc.getPosition();//收费段的起点
						String[] pos=position.split(",");
						  double pos1=Double.parseDouble(pos[0]);	double pos2=Double.parseDouble(pos[1]);//收费点起点的经纬度   
						  String endPosition =cc.getEndPosition();//收费段的终点
						  String[] endPos=endPosition.split(",");
						  double endPos1=Double.parseDouble(endPos[0]);	double endPos2=Double.parseDouble(endPos[1]);//收费点出口的经纬度 
						  double starting_point=Distance.getDistance(driverpos1, driverpos2, pos1, pos2);//司机的当前位置与收费站起点做对比
						  double starting_end_point=Distance.getDistance(driverpos1, driverpos2, endPos1, endPos2);
						  if(starting_point<=50){
							  String carCaculationPointCharges_line=  jedis.hget("carCaculationPointCharges","carCaculationPointCharges_line_"+orderId);//获取之前是否有收费点记录
						      if(!StringTools.isEmpty(carCaculationPointCharges_line)){
						    		  List<Map<String,List<String>>> lisMap = JSONArray.fromObject(carCaculationPointCharges_line);//数据结构如下[{"朗山路":["经纬度","经纬度"]}]
						    		  Map<String,List<String>> mapList = lisMap.get(lisMap.size()-1);
						    		 List<String> roadString= mapList.get(cc.getRoadName());//该高速上途径过的收费点经纬度
						    		 if(roadString!=null){
						    			 String roadStrin=roadString.toString();//改线路中
							    		 if(roadStrin.indexOf(str)==-1){//我的收费点线路中未出现该收费点
							    			 roadString.add(str);
						    				 roadString.add(endPosition);
							    		 }
						    		 }else{
						    			 roadString = new ArrayList<>();
//						    			 roadString.add(str);
						    			if(starting_end_point<=50){//结束距离小于50
						    				 roadString.add(str);
						    				 roadString.add(endPosition);
						    			} 
						    		 }
						    		 mapList.put(cc.getRoadName(), roadString);
						    		 lisMap.add(mapList);
						    		 JSONArray content = JSONArray.fromObject(lisMap);
						    		 String newContent = content.toString();
						    		 jedis.hset("carCaculationPointCharges", "carCaculationPointCharges_line_"+orderId + "", newContent);
						      }else{
						    	  List<Map<String,List<String>>> lisMap =new ArrayList<>();
						    	  Map<String,List<String>> mapList =new HashMap<>();
						    	  List<String> roadString=new ArrayList<>();
						    	  roadString.add(str);
						    	  roadString.add(endPosition);
						    	  mapList.put(cc.getRoadName(), roadString);
						    	  lisMap.add(mapList);
						    	  JSONArray content = JSONArray.fromObject(lisMap);
						    	 String newContent = content.toString();
						    	 jedis.hset("carCaculationPointCharges", "carCaculationPointCharges_line_"+orderId + "", newContent);
						      }
						  }
				   }
			  }
			}else{//如果缓存中没有收费站线的数据 查询使用
					Map<String,Object> map1 = new HashMap<>();
					map1.put("type", 2);
					carCaculationPointList = carCaculationPointChargesMapper.getList(map1);
					line = JSONArray.fromObject(line).toString();
					jedis.hset("carCaculationPointCharges","carCaculationPointCharges_line",line);
			}*/
		
		
		
		
		jedis.close();
		jedisCluster.close();
	}

}
